/**
 * Created by jiangzhiyong on 15/10/14.
 */

var Manage = {
    init : function() {
        Manage.get_data();
        $("#data_table tbody").find("a[name='update_category']").click(Manage.category_show);
        $("a[name='add_category']").click(Manage.category_show);
        $("#data_table tbody").find("a[name='add_class']").click(Manage.class_show);
        $("#data_table tbody").find("a[name='remove_class']").click(function(){
        		var category_id = $(this).attr("category_id");
	    		var class_id = $(this).attr("class_id");
	    		ajax("/categories/classes/remove", {"category_id": category_id, "class_ids" : class_id}, success);
        });
        $("#do_update_category").click(function(){
        		var category_id = $("#update_category").find("input[name='category_id']").val();
        		var category_name = $("#update_category").find("input[name='category_name']").val();
        		if (category_name == null || category_name == "") {
        			alert("课程类目名称不能为空");
        			return false;
        		}
        		if(category_id == "" || category_id == 0) {
        			//新增
        			ajax("/categories/create", {"name" : category_name}, success);
        		} else {
        			//编辑
        			ajax("/categories/update", {"category_id": category_id, "name" : category_name}, success);
        		}
        });
        $("#do_add_class").click(function(){
        		var category_id = $("#add_class").find("input[name='category_id']").val();
        		var class_id = $("#add_class").find("select[name='class_id']").val();
        		if (class_id == null || class_id == "") {
        			alert("必须选择一个科目");
        		}
    			ajax("/categories/classes/add", {"category_id": category_id, "class_ids" : class_id}, success);
        });
    },
    get_data : function() {
        ajax("/categories/list", {}, callback_init_table);
    },
    category_show : function() {
    		var category_id = $(this).attr("category_id");
    		$("#update_category").find("input[name='category_id']").val(category_id);
    		var category_name = $(this).attr("category_name");
    		$("#update_category").find("input[name='category_name']").val(category_name);
    		$("#update_category").modal('show');
    },
    class_show : function() {
    		var category_id = $(this).attr("category_id");
    		$("#add_class").find("input[name='category_id']").val(category_id);
    		ajax("/classes/list", {}, callback_init_class);
    		$("#add_class").modal('show');
    },
    init_table : function(result) {
    		if (result != null && result.data != null && result.data.categories.length > 0) {
    			var tbody = $("#data_table tbody");
    			tbody.empty();
    			var categories = result.data.categories;
    			var trs = "";
    			for (var i=0; i < categories.length; i++) {
    				var category = categories[i];
    				var classes = category.classes;
    				if (classes == null) {
    					classes = [];
    				}
    				trs += ("<tr>");
    				var categoryName = ("<td rowspan=" + classes.length +">" + category.name +"</td>");
    				var categoryStatus = ("<td rowspan=" + classes.length +">正常</td>");
    				var categoryOperate = ("<td rowspan=" + classes.length +">" + 
    										"<a category_id=" + category.id + " category_name=" + category.name + " name='update_category' href='javascript:void(0)'>更新课程类目</a>" +
    										"<br/><a category_id=" + category.id + " name='add_class' href='javascript:void(0)'>添加科目</a>" +
    										"</td>");
    				trs += (categoryName + categoryStatus);
    				if (classes.length == 0) {
    					trs += ("<td> - </td><td> - </td><td> - </td>");
    					trs += (categoryOperate + "</tr>");
    				} else {
	    				for (var j=0; j < classes.length; j++){
	    					var classOne = classes[j];
	    					if (j > 0) {
	    						trs += ("<tr>");
	    					}
	    					var className = ("<td>" + classOne.name +"</td>");
	    					var classStatus = ("<td>正常</td>");
	    					var classOperate = ("<td><a category_id=" + category.id + " class_id=" + classOne.id + " name='remove_class' href='javascript:void(0)'>移除科目</a></td>");
	    					trs += (className + classStatus + classOperate);
	    					if (j == 0) {
	    						trs += (categoryOperate + "</tr>");
	    					} else {
	    						trs += ("</tr>");
	    					}
	    				}
	    				trs += ("</tr>");
    				}
    			}
    			if (categories.length == 0) {
				trs += ("<tr>");
				trs += ("<td colspan='6'>未查询到任何结果</td>");
				trs += ("</tr>");
			}
    			tbody.append(trs);
    			
    		}
    },
    init_class : function(result) {
    		if (result != null && result.data != null && result.data.classes.length > 0) {
    			var class_id = $("#add_class").find("[name=class_id]");
    			class_id.empty();
    			var classes = result.data.classes;
    			var options = "";
    			for (var i=0; i < classes.length; i++) {
    				var classOne = classes[i];
    				options += ("<option value = " + classOne.id +">" + classOne.name + "</option>");
    			}
    			class_id.append(options);
    		}
    }
};

function callback_init_table(result) {
	Manage.init_table(result);
}

function callback_init_class(result) {
	Manage.init_class(result);
}

function success(result) {
	if (result != null && result.code == 0) {
		window.location.reload();
	} else {
		alert("操作失败，请刷新页面重试！");
	}
}

$(function(){
    Manage.init();
})
